package com.ljz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljz.entity.User;
import com.ljz.mapper.UserMapper;
import com.ljz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean login(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User u = userMapper.login(user);
        if (u != null) {
            return true;
        }
        return false;
    }
    @Override
    public boolean register(User user) {
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public boolean checkUsername(String username) {
        int result = userMapper.checkName(username);
        return result > 0;
    }

    @Override
    public int deleteUsers(List<Integer> ids) {
        return userMapper.deleteUserById(ids);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public PageInfo<User> findUsers(String username, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = (Page<User>) userMapper.findAllByPage(username);
        return page.toPageInfo();
    }
}
